package jMiiEditor.myavatareditor.xml;

import java.io.IOException;
import utils.BinStuffs;

public class XMLAppender implements Appendable
{
    Appendable a;
    
    public XMLAppender( Appendable a )
    {
        this.a = a;
    }
    
    @Override
    public Appendable append( CharSequence csq ) throws IOException
    {
        this.a.append( csq );
        return this;
    }

    @Override
    public Appendable append( char c ) throws IOException
    {
        this.a.append( c );
        return this;
    }

    @Override
    public Appendable append( CharSequence csq, int start, int end )
            throws IOException
    {
        this.a.append( csq, start, end );
        return this;
    }
    
    public Appendable appendLine( 
            int     tabCount,
            String  xmlElementStart,
            String  xmlValue,
            String  xmlElementEnd
            )
        throws IOException
    {
        for( int i = 0; i<tabCount; i++ ) {
            this.a.append( "  " );
        }
        
        this.a.append( xmlElementStart.trim() );
        
        if( xmlValue != null ) {
            this.a.append( xmlValue.trim() );
        }
        
        if( xmlElementEnd != null ) {
            this.a.append( xmlElementEnd.trim() );
        }
        
        this.a.append( "\n" );
        
        return this;
    }
    
    public Appendable appendLine( 
            int     tabCount,
            String  xmlElementStartEnd
            )
        throws IOException
    {
        return appendLine( 
                tabCount, 
                xmlElementStartEnd, 
                (String)null, 
                (String)null
                );
    }
    
    public Appendable appendLine( 
            int     tabCount,
            String  xmlElementStart,
            byte[]  xmlValue,
            String  xmlElementEnd
            )
        throws IOException
    {
        return appendLine( 
                tabCount, 
                xmlElementStart, 
                BinStuffs.toHexString( xmlValue ), 
                xmlElementEnd
                );
    }
    
    public Appendable appendLine( 
            int     tabCount,
            String  xmlElementStart,
            int     xmlValue,
            String  xmlElementEnd
            )
        throws IOException
    {
        return appendLine( 
                tabCount, 
                xmlElementStart, 
                Integer.toString( xmlValue ), 
                xmlElementEnd
                );
    }
    
    public Appendable appendLine( 
            int     tabCount,
            String  xmlElementStart,
            boolean xmlValue,
            String  xmlElementEnd
            )
        throws IOException
    {
        return appendLine( 
                tabCount, 
                xmlElementStart, 
                xmlValue ? "1" : "0", 
                xmlElementEnd
                );
    }
}